package com.itqf.config;

import com.itqf.realm.UserRealm;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

/**
 * @author 徐老板
 * @date 2020/1/29   11:25
 */
@Configuration
public class ShiroConfig {

    @Bean
    public SessionManager sessionManager(){
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        sessionManager.setGlobalSessionTimeout(1000*60*60);
        sessionManager.setSessionValidationSchedulerEnabled(true);
        return sessionManager;
    }

    @Bean
    public SecurityManager securityManager(UserRealm realm, SessionManager sessionManager){
       DefaultWebSecurityManager securityManager =new DefaultWebSecurityManager();
       securityManager.setRealm(realm);
       securityManager.setSessionManager(sessionManager);

       EhCacheManager ehCacheManager =new EhCacheManager();
       ehCacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
       securityManager.setCacheManager(ehCacheManager);

        CookieRememberMeManager rememberMeManager=new CookieRememberMeManager();
        Cookie cookie = rememberMeManager.getCookie();
        cookie.setMaxAge(60*60*24*30);
        securityManager.setRememberMeManager(rememberMeManager);

        return securityManager;

    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        LifecycleBeanPostProcessor lifecycleBeanPostProcessor =new LifecycleBeanPostProcessor();
        return lifecycleBeanPostProcessor;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator proxyCreator =new DefaultAdvisorAutoProxyCreator();
        /**
         * 添加配置使shiro注解生效
         */
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilter=new ShiroFilterFactoryBean();

        shiroFilter.setSecurityManager(securityManager);

        shiroFilter.setLoginUrl("/login.html");
        shiroFilter.setSuccessUrl("/index.html");
        shiroFilter.setUnauthorizedUrl("/unauthorized.html");

//        上面配页面下面配路径
        LinkedHashMap<String,String> map=new LinkedHashMap<>();
        map.put("/sys/login", "anon");
        //验证码
        map.put("/captcha.jpg","anon");
        //静态js  css
        map.put("/public/**","anon");
        //假数据
        map.put("/json/**","anon");
        map.put("/demo/**","anon");
        //定时任务
        map.put("/schedule/**","anon");

//        map.put("/statics/**", "anon");

//        map.put("/sys/menu/*","perms[\'sys:menu\']" );
        map.put("/**","authc" );
        map.put("/**","user" ); //选择记住我

        shiroFilter.setFilterChainDefinitionMap(map);
        return shiroFilter;
    }









}
